package com.example.anpnovel.controller.crud;


import com.example.anpapi.entity.po.novelmodel.ReadingRecords.ReadingRecord;
import com.example.anpapi.entity.vo.R;
import com.example.anpnovel.service.interfaces.ReadingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/reading-record")
public class ReadingRecordController {

    @Autowired
    private ReadingRecordService readingRecordService;

    /**
     * 阅读记录添加
     * @param readingRecord
     * @return
     */
    @PostMapping("/save")
    public R<ReadingRecord> save(@RequestBody ReadingRecord readingRecord){
        log.info("阅读记录添加:{}", readingRecord);
        readingRecordService.save(readingRecord);
        return R.success(readingRecord,"阅读记录添加成功");
    }

    /**
     * 根据记录id删除阅读记录
     * @param readingRecord
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody ReadingRecord readingRecord){
        log.info("阅读记录删除:{}", readingRecord);
        if(readingRecordService.getById(readingRecord.getId()) != null)
        {
            readingRecordService.removeById(readingRecord.getId());
            return R.success(null,"阅读记录删除成功");
        }
        return R.error("未找到该阅读记录");
    }

    /**
     * 根据记录id查找阅读记录
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<ReadingRecord> get(@RequestParam(value = "id") Long id) {
        log.info("查询阅读记录的id为{}", id);
        ReadingRecord readingRecord = readingRecordService.getById(id);
        return R.success(readingRecord, "查询阅读记录成功");
    }

    /**
     * 根据用户id和章节id查找阅读记录
     * @param userId
     * @param chapterId
     * @return
     */
    @GetMapping("/getByUserAndChapter")
    public R<ReadingRecord> getByUserAndChapter(@RequestParam(value = "userId") Long userId, 
                                                  @RequestParam(value = "chapterId") Long chapterId) {
        log.info("查询阅读记录 - userId:{}, chapterId:{}", userId, chapterId);
        ReadingRecord readingRecord = readingRecordService.lambdaQuery()
                .eq(ReadingRecord::getUserId, userId)
                .eq(ReadingRecord::getChapterId, chapterId)
                .one();
        return R.success(readingRecord, "查询阅读记录成功");
    }

    /**
     * 根据用户id查找所有阅读记录
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public R<java.util.List<ReadingRecord>> getByUserId(@RequestParam(value = "userId") Long userId) {
        log.info("查询用户{}的所有阅读记录", userId);
        java.util.List<ReadingRecord> readingRecords = readingRecordService.lambdaQuery()
                .eq(ReadingRecord::getUserId, userId)
                .orderByDesc(ReadingRecord::getUpdatedTime)
                .list();
        return R.success(readingRecords, "查询阅读记录成功");
    }

    /**
     * 修改阅读记录
     * @param readingRecord
     * @return
     */
    @PostMapping("/update")
    public R<ReadingRecord> update(@RequestBody ReadingRecord readingRecord){
        log.info("修改的阅读记录为{}", readingRecord);
        readingRecordService.updateById(readingRecord);
        return R.success(readingRecord,"修改阅读记录成功");
    }

}

